//---------------------------------------------------------------------------

#ifndef CIISLServerInterfaceH
#define CIISLServerInterfaceH

#include "CIISDB.h"
#include "DebugWinU.h"
#include <scktcomp.hpp>

//---------------------------------------------------------------------------

enum CIISEvTabStatus { ETSMissingRec, ETSPrjCh, ETSCtrlCh, ETSZoneCh, ETSPSCh,
                      ETSRecCh, ETSSensorCh };
enum CIISPrjTabStatus { PTSLocalError, PTSMissingRec, PTSPrjNoDontMatch };

enum CIISCtrlTabStatus { CTSLocalError, CTSMissingRec };

enum CIISZoneTabStatus { ZTSLocalError, ZTSMissingRec };

enum CIISSensTabStatus { STSLocalError, STSMissingRec };

enum CIISPSTabStatus { PSTSLocalError, PSTSMissingRec };

enum CIISRecTabStatus { RecTSLocalError, RecTSMissingRec };

enum CIISValTabStatus { ValTSLocalError, ValTSMissingRec };

enum CIISLPRTabStatus { LPRTSLocalError, LPRTSMissingRec };

enum CIISDecayTabStatus { DecayTSLocalError, DecayTSMissingRec };

class TCIISLServerInterface : public TObject
{
private:
  TCIISDBModule *DB;
  TDebugWin *DW;
  int PortNo;
  TList *MessageList, *ResponseList;
  TClientSocket *iSocket;
  TCustomWinSocket * LServerSocket;
  int MessageNumber;
  unsigned char SocketInBuf[SocketInBufSize];
  unsigned char SocketOutBuf[SocketOutBufSize];
  unsigned char SocketInputQueue[SocketInputQueueSize];
  int SocketInputIdx;

  CIISCheckSyncState CheckSyncState;
  CIISDataSyncState DataSyncState;
  CIISSysSyncState SysSyncState;
  CIISEventSyncState EventSyncState;
  CIISProjectSyncState ProjectSyncState;
  CIISCtrlSyncState CtrlSyncState;
  CIISZoneSyncState ZoneSyncState;
  CIISSensorSyncState SensSyncState;
  CIISPSSyncState PSSyncState;
  CIISRecSyncState RecSyncState;
  CIISValSyncState ValSyncState;
  CIISLPRSyncState LPRSyncState;
  CIISDecaySyncState DecaySyncState;

  TDateTime TOut;
  bool RequestTimeOut;
  int RequestNo;

  Set < CIISEvTabStatus, ETSMissingRec, ETSSensorCh > EvTabStatusSet;
  Set < CIISPrjTabStatus, PTSLocalError, PTSPrjNoDontMatch > PrjTabStatusSet;
  Set < CIISCtrlTabStatus, CTSLocalError, CTSMissingRec > CtrlTabStatusSet;
  Set < CIISZoneTabStatus, ZTSLocalError, ZTSMissingRec > ZoneTabStatusSet;
  Set < CIISSensTabStatus, STSLocalError, STSMissingRec > SensTabStatusSet;
  Set < CIISPSTabStatus, PSTSLocalError, PSTSMissingRec > PSTabStatusSet;
  Set < CIISRecTabStatus, RecTSLocalError, RecTSMissingRec > RecTabStatusSet;
  Set < CIISValTabStatus, ValTSLocalError, ValTSMissingRec > ValTabStatusSet;
  Set < CIISLPRTabStatus, LPRTSLocalError, LPRTSMissingRec > LPRTabStatusSet;
  Set < CIISDecayTabStatus, DecayTSLocalError, DecayTSMissingRec > DecayTabStatusSet;
  
  TLocateOptions NoOpts;

  
        bool FLServerConnected;
        bool FConfigChanged;
        bool FDataChanged;
        bool FLastValChanged;
        void __fastcall Debug(String Message);
        bool __fastcall EventTabSync(void);
        bool __fastcall SyncAll();
        int __fastcall Request( CIISCommand Cmd,
                                          CIISTable Tab,
                                          String Data);
        TCamurPacket* __fastcall GetReply(int RequestNo);
        void __fastcall SetEvTabStatus(CIIEventCode EvCode);
        void __fastcall SendMessageToSocket(TCamurPacket *Request);
        void __fastcall AddSocketInputQueue(char* Buf, int N);
        bool __fastcall ParseMessages();
        bool __fastcall ParseCommands();
        void __fastcall EventTabSyncIni();
        void __fastcall ProjectTabSyncIni();
        bool __fastcall ProjectTabSync();
        void __fastcall ProjectTabCeckIni();
        bool __fastcall ProjectTabCheck();
        void __fastcall SystemSyncSelectStep();
        void __fastcall CtrlTabSyncIni();
        bool __fastcall CtrlTabSync();
        void __fastcall ZoneTabSyncIni();
        bool __fastcall ZoneTabSync();
        void __fastcall SensorTabSyncIni();
        bool __fastcall SensorTabSync();
        void __fastcall PSTabSyncIni();
        bool __fastcall PSTabSync();
        void __fastcall RecordingTabSyncIni();
        bool __fastcall RecordingTabSync();
        void __fastcall ValueTabSyncIni();
        bool __fastcall ValueTabSync();
        void __fastcall ValueLPRTabSyncIni();
        bool __fastcall ValueLPRTabSync();
        void __fastcall ValueDecayTabSyncIni();
        bool __fastcall ValueDecayTabSync();


public:
        __fastcall TCIISLServerInterface(TCIISDBModule * SetDB,
                                         TDebugWin * SetDebugWin,
                                         int SetPortNo);
        __fastcall ~TCIISLServerInterface();
        void __fastcall LServerConnect(TObject * Sender, TCustomWinSocket * Socket);
        void __fastcall LServerLookup(TObject * Sender, TCustomWinSocket * Socket);
        void __fastcall LServerDisconnect(TObject * Sender, TCustomWinSocket * Socket);
        void __fastcall LServerRead(TObject * Sender, TCustomWinSocket * Socket);
        void __fastcall SetDebugWin(TDebugWin * SetDebugWin);
        bool __fastcall SystemSync();
        bool __fastcall SystemSyncIni();
        bool __fastcall DataSyncIni();
        bool __fastcall DataSync();
        bool __fastcall CheckSyncIni();
        bool __fastcall CheckSync();
        
__published:
        __property bool LServerConnected  = { read=FLServerConnected };
        __property bool ConfigChanged  = { read=FConfigChanged };
        __property bool DataChanged  = { read=FDataChanged };
        __property bool LastValChanged  = { read=FLastValChanged };

};

#endif
